<?php
namespace app\api\controller;

use app\api\Base;
use think\Config;
use think\Exception;
use think\Db;
use com\gdl\newagk\interfaces\PlaformServiceClient;
use com\gdl\newagk\dataobject\plaform\PlaformDTO;

class Plaform extends Base
{

    public function getPlaform()
    {
        $msg = ['code'=>400,'msg'=>'暂无平台','data'=>[],'count'=>0];
        $level = $this->request->param('level') ? $this->request->param('level') : 1;
        $offset = $this->request->param("page") ? $this->request->param("page") : 0;
        $num = $this->request->param("pagesize") ? $this->request->param("pagesize") : 10;
        if ($level) {
            $userid = $this->request->param('userid') ? $this->request->param('userid') : '';

            try {
                \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
                $UserService = new PlaformServiceClient(NULL);
                \ApiClient::build($UserService);
                # plaformType 为分页 本身等于0
                //$result = $UserService->getPlaform(0, 0, $userid, 1);
                $result = $UserService->getPlaform($offset, 0, $userid, 1);
                if ($result->success == TRUE) {
                    $msg['code'] = 400;
                    $msg['data'] = [];
                    if ($result->code == 200) {
                        $msg['msg'] = '获取成功';
                        $msg['code'] = $result->code;
                        $msg['data'] = $result->data ? $result->data : [];
                        $msg['count'] = intval($result->message);
                    }
                }
            } catch (Exception $ex) {
                \SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
            }
            // $plaform['length'] = count($plaform);
            // return json_encode($plaform);
        } else {
            $name = $this->request->param('name') ? $this->request->param('name') : '';
            try {
                \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
                $UserService = new PlaformServiceClient(NULL);
                \ApiClient::build($UserService);
                $result = $UserService->getPlaform(2, $name);

                if ($result->success == TRUE) {
                    $msg['code'] = 400;
                    $msg['msg'] = "没有选择任何平台";
                    $msg['data'] = [];
                    if ($result->code == 200) {
                        $msg['code'] = $result->code;
                        $msg['data'] = $result->data;
                    }
                }
            } catch (Exception $ex) {
                \SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
            }
            // $plaform['length'] = count($plaform);
            // return json_encode($plaform);
        }
        return json_encode($msg);
    }

    /**
     * 根据平台ID得出平台信息
     * @param $plaform_id 
     * @return type
     */
    public function getPlaformInfoById() 
    {
        $msg = ['code'=>400,'msg'=>'没有数据','data'=>[]];
        $plaform_id = $this->request->param('plaform_id') ? $this->request->param('plaform_id') : '';
        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
            $plaformService = new PlaformServiceClient(NULL);
            \ApiClient::build($plaformService);
            $result = $plaformService->getPlaformInfoById($plaform_id);
            if($result->code == 1){
                $msg['code'] = 200; 
                $msg['data'] = $result->data;
                $msg['msg'] = "获取成功";
            }
            
        } catch (Exception $ex) {
            \SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
        return json_encode($msg);
    }

    /**
     * 平台邀请码使用
     * @outputBuffering 
     * @param $code 
     */
    public function plaformInvitCode() 
    {
        $msg = ['code'=>400,'msg'=>'邀请码不存在','data'=>[]];
        $code = $this->request->param('code') ? getStr($this->request->param('code'), 'string') : '';
        $userid = $this->request->param('gdl_userid') ? getStr($this->request->param('gdl_userid'),'int') : 0;
        if(empty($code)){
            $msg['msg'] = "邀请码不能为空";
            return json_encode($msg);
        }
        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
            $plaformService = new PlaformServiceClient(NULL);
            \ApiClient::build($plaformService);
            $result = $plaformService->plaformInviteCodeConfirm($code,$userid);
            if ($result->success==true) {
                $msg['msg'] = $result->message;
                if($result->code == 200){
                    $msg['code'] = 200;
                    $msg['data']= $result->data;
                    //$msg['data'] = ['code'=>$result->invitecode];
                }

            }
        } catch (Exception $ex) {
            \SLogger::getLogger('plaform_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
        return json_encode($msg);
    }

    
    
        /**
     * 平台二维码激活
     * @outputBuffering 
     * @param $code 
     */
    public function plaformInviteActive() 
    {
        $str = getStr($this->request->param('str'));
        if ($str = explode('|', $str)) {
            if ($str[0] == 'id') {
                $plaform =  Db::name('plaform_invitecode')->where(['plaform_id' => $str[1], 'status' => 1, 'expire_time' > $this->time])->field('code')->find();
            } elseif ($str[0] == 'code') {
                $code = $str[1];
            }
        }

        $userid = $this->request->param('userid') ? getStr($this->request->param('userid'), 'int') : 0;
        try {
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
            $plaformService = new PlaformServiceClient(NULL);
            \ApiClient::build($plaformService);
            $result = $plaformService->plaformInviteCodeConfirm($code, $userid);
            if ($result->success) {
                $this->ajaxReturn($result, 'JSON');
            } else {
                \SLogger::getLogger('plaform_thrift_service', 'thrift_error')->error('error return');
            }
        } catch (Exception $ex) {
            \SLogger::getLogger('plaform_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
    }
    /**
     * 选择平台 
     */
    public function selectPlaform(){
        $msg = ['code'=>400, 'msg'=>'选择平台失败', 'data'=>[]];
        $plaformId = $this->request->param("plaform_id") ? $this->request->param("plaform_id") : "";
        $userId = $this->request->param("userid") ? $this->request->param("userid") : "";

        if(empty($plaformId) || empty($userId)){
            $msg['msg'] = "缺少必要参数";
            return json_encode($msg);
        }
        try{
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlAgkSdk'));
            $plaformService = new PlaformServiceClient(NULL);
            \ApiClient::build($plaformService);
            $plaformDo = new PlaformDTO();
            $plaformDo->plaformid = $plaformId;
            $plaformDo->userid = $userId;

            $result = $plaformService->selectPlaform($plaformDo);
            if($result->success == true){
                $msg['code'] = $result->code;
                $msg['msg'] = $result->message;
                if($result->code ==200){
                    $msg['data']= $result->data[0];    
                }
            }

        }catch(Exception $ex){
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    }

}
